package com.service.city.impl;

import com.bean.City;
import com.bean.Prop_config;
import com.common.Cache;
import com.dao.CityMapper;
import com.dao.Prop_configMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.city.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lvjianqing on 2017/9/10.
 */
@Service
public class CityServiceImpl implements CityService {
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private Prop_configMapper prop_configMapper;


    @Override
    public Page findByPage(int pageNum, int pageSize, HashMap<String, Object> paraMap) {
        PageHelper.startPage(pageNum, pageSize);
        Page page = (Page) cityMapper.findAll(paraMap);
        return page;
    }

    @Override
    public City findCityById(Integer id) {
        if (id != null) {
            return cityMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public void saveCity(City city) {
        if (city != null) {
            if (city.getId() != null) {
                cityMapper.updateByPrimaryKeySelective(city);
            } else {
                city.setCreatetime(new Date());
                cityMapper.insertSelective(city);
            }
            //刷新城市缓存
            Cache.initCityMap();
        }
    }

    @Override
    public List<City> selectList(HashMap<String, Object> paraMap) {
        return cityMapper.selectList(paraMap);
    }

    @Override
    public boolean update(City city) {
        if (city == null || city.getId() == null) {
            return false;
        }
        int i = cityMapper.updateByPrimaryKeySelective(city);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Prop_config getPropByCityId(Integer id) {
        if (id != null) {
            return prop_configMapper.findByCityid(id);
        }
        return null;
    }

    @Override
    public void saveProp(Prop_config prop) {
        if (prop != null) {
            if (prop.getId() != null) {
                prop_configMapper.updateByPrimaryKeySelective(prop);
            } else {
                prop_configMapper.insertSelective(prop);
            }
        }
    }
}
